package com.qdlc.p2p.quartz;


import java.text.ParseException;

import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;

public class QuartzManage {

    private static final Logger logger = Logger.getLogger("job");

    /**
     * 从Scheduler 移除当前的Job,修改Trigger
     *
     * @param jobExecutionContext
     * @param time
     * @throws SchedulerException
     * @throws ParseException
     */
    public static void modifyJobTime(JobExecutionContext jobExecutionContext, String time)
            throws SchedulerException, ParseException {
        Scheduler scheduler = jobExecutionContext.getScheduler();
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        String jobName = jobDetail.getName();
        String jobGroup = jobDetail.getGroup();
        CronTrigger ct = (CronTrigger) jobExecutionContext.getTrigger();
        // 移除当前进程的Job
        scheduler.deleteJob(jobName, jobGroup);
        // 修改Trigger
        ct.setCronExpression(time);
        // 重新调度jobDetail
        scheduler.scheduleJob(jobDetail, ct);
        logger.info(jobName.concat(" modify execute time ").concat(time));
    }
}
